<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>5.片元着色器-绘制 HSV 色轮</title>
    <style>
        canvas{
            border:1px solid #aaa;
            margin: 0 auto;
            display: block;
        }
    </style>
</head>
<body>
    <canvas width="554" height="554"></canvas>
    <script src="/common/lib/gl-renderer.js"></script>
    <script type="module">
        import { loadImage, toPolar, fromPolar } from "/common/lib/utils.js";
        
        /**
         * gl-renderer五步法
         * 1.创建renderer对象
         * 2.创建并启用webgl程序
         * 3.设置uniform变量
         * 4.将定点数据送入缓冲区
         * 5.渲染
         * 
        */
        // 1.
        let canvas = document.querySelector("canvas");
        const renderer = new GlRenderer(canvas);

        // 2.
        const vertex = `
            attribute vec2 a_vertexPosition;
            attribute vec2 uv;
            varying vec2 vUv;

            void main(){
                gl_PointSize = 1.0;
                vUv = uv;
                gl_Position = vec4(a_vertexPosition, 1.0, 1.0);
            }
        `
        const fragment = `
            #ifdef GL_ES
            precision mediump float;
            #endif

            uniform vec2 uMouse;
            varying vec2 vUv;

            vec3 hsv2rgb(vec3 c){ 
                vec3 rgb = clamp(abs(mod(c.x*6.0+vec3(0.0,4.0,2.0), 6.0)-3.0)-1.0, 0.0, 1.0); 
                rgb = rgb * rgb * (3.0 - 2.0 * rgb); 
                return c.z * mix(vec3(1.0), rgb, c.y);
            }
            vec2 polar(vec2 st){
                // 极径跟弧度
                return vec2(length(st), atan(st.y, st.x));
            }
            
            void main() {
                vec2 st = vUv - vec2(0.5);
                st = polar(st);
                float d = smoothstep(st.x, st.x + 0.01, 0.2);
                // 角度范围映射到0-2Π
                if(st.y < 0.0) st.y += 6.28;

                // 计算p的值，也就是相对角度,取值0-1
                float p = st.y / 6.28;

                // p表示HUE值，uMouse.x表示亮度，uMouse.y表示饱和度
                gl_FragColor.rgb = d * hsv2rgb(vec3(p, uMouse.x, uMouse.y));

                gl_FragColor.a = 1.0;
            }
        `

        const program = renderer.compileSync(fragment,vertex)
        renderer.useProgram(program)
        // 3.
        renderer.uniforms.uMouse = [1, 1]
        // 4.
        renderer.setMeshData([
            {
                positions:[
                    [-1,-1],
                    [-1,1],
                    [1,1],
                    [1,-1],
                ],
                attributes:{
                    uv:[
                        [0,0],
                        [0,1],
                        [1,1],
                        [1,0],
                    ]
                },
                cells:[[0,1,2],[2,0,3]]
            }
        ])
        // 5.
        renderer.render()

    </script>
</body>
</html>